﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Networking
{

    /// <summary>
    /// Packet of type AutenticationPacket, the first packet in autentication process which contains the challenge bytes
    /// </summary>
    public class AutenticationPacket : Packet
    {

        /// <summary>
        /// The challenge bytes
        /// </summary>
        private int _Challenge;

        /// <summary>
        /// A copy of the challenge bytes used in autentication process
        /// </summary>
        public int Challenge
        {
            get
            {
                return _Challenge; // (byte[])_Challenge.Clone();
            }
        }

        /// <summary>
        /// Creates a new instance of PacketAutentication with the provided challenge bytes
        /// </summary>
        /// <param name="Challenge">The challenge bytes</param>
        public AutenticationPacket(int Challenge)
            : base(PacketType.Autentication)
        {
            _Challenge = Challenge;
            //Challenge.CopyTo(_Challenge, 0);
        }

        ///// <summary>
        ///// Creates a new instance of PacketAutentication with a random challenge of len ChallengeLength
        ///// </summary>
        ///// <param name="ChallengeLength">The length of the random challenge</param>
        //public AutenticationPacket(int ChallengeLength)
        //    : base(PacketType.Autentication)
        //{
        //    _Challenge = new byte[ChallengeLength];

        //    (new Random()).NextBytes(_Challenge);
            
        //}

        /// <summary>
        /// Creates a new instance of PacketAutentication with a random 16-bytes challenge
        /// </summary>
        public AutenticationPacket()
            : base(PacketType.Autentication)
        {
            _Challenge = (new Random()).Next();
        }

        /// <summary>
        /// Creates a new instance of AutenticationPacket extracting all the parameters from the byte buffer
        /// </summary>
        /// <param name="buffer">The byte buffer containing the packet data</param>
        public AutenticationPacket(byte[] buffer)
            : base(PacketType.Autentication, buffer)
        {
            //
        }

        /// <summary>
        /// Serializes the packet in order to send it
        /// </summary>
        /// <returns>The bytes of the packet</returns>
        public override byte[] Serialize()
        {
            return BitConverter.GetBytes(_Challenge);
        }

        /// <summary>
        /// Loads a packet from a byte buffer
        /// </summary>
        /// <param name="buffer">The data buffer</param>
        public override void Load(byte[] buffer)
        {
            if (buffer.Length != 4) throw new BufferSizeMismatchException(4);

            this._Challenge = BitConverter.ToInt32(buffer, 0);
        }


    }
}
